/*
 * @Author: jiangjinchi 1468922694@qq.com
 * @Date: 2023-10-31 08:40:35
 * @LastEditors: jiangjinchi 1468922694@qq.com
 * @LastEditTime: 2025-10-10 09:24:50
 * @FilePath: \vue-admin-template\src\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import getPageTitle from '@/utils/get-page-title'
import Cookies from 'js-cookie';
import Layout from '@/layout'
const _import = require('./router/_import_' + process.env.NODE_ENV) // 获取组件的方法
NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  // const hasToken = Cookies.get('tokenValueEqu')
  const hasToken = 1


  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      // const hasGetUserInfo = store.getters.name
      const hasGetUserInfo = 2


      if (hasGetUserInfo) {
        next()
      } else {
        try {
          // await store.dispatch('user/getInfo')

          // if (store.getters.menus.length < 1) {
          //   global.antRouter = []
          //   next()
          // }
          // const menus = filterAsyncRouter(store.getters.menus)
          // router.addRoutes(menus)

          // global.antRouter = menus
          // next({
          //   ...to,
          //   replace: true
          // })
          next()
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
function filterAsyncRouter(asyncRouterMap) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.component) {
    
      if (route.component == 'Layout') {
        route.component = Layout
      } else {
        route.component = _import(route.component)
      }
    }
    // route.children = route.children || [];
    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children)
    }
    return true
  })
  return accessedRouters
}
